package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.BrandSettingsBean;
import cc.rengu.igas.bsps.facade.request.QueryPhoneSettingsRequest;
import cc.rengu.igas.bsps.facade.response.QueryPhoneSettingsResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 手机型号查询模板
 *
 * @author zhangxuran
 * @version 1.0
 * @date 2020.08.04
 */
public class QueryPhoneSettingsService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {

        QueryPhoneSettingsRequest queryPhoneSettingsRequest = new QueryPhoneSettingsRequest();
        ConvertUtil.convertOutput(queryPhoneSettingsRequest);
        return queryPhoneSettingsRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        QueryPhoneSettingsRequest queryPhoneSettingsRequest = (QueryPhoneSettingsRequest) request;
        String phoneBrandModel = queryPhoneSettingsRequest.getPhoneBrandModel();
        String instId = queryPhoneSettingsRequest.getHeader().getInstId();
        if (StringUtil.isEmptyOrNull(phoneBrandModel)) {
            rglog.error("必填性校验失败！phoneBrandModel<{}>", phoneBrandModel);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam modelSysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, BspsAppParamConstant.PHONE_BRAND_MODEL, phoneBrandModel);
        List<BrandSettingsBean> brandSettingsBeanList = new ArrayList();
        //没有型号的时候选择其他
        if (modelSysParam == null || StringUtil.isEmptyOrNull(modelSysParam.getParamValue())) {
            List<SysParam> sysParamList = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, BspsAppParamConstant.OTHER);
            if (sysParamList != null) {
                for (int i = 0; i < sysParamList.size(); i++) {
                    SysParam sysParam = sysParamList.get(i);
                    BrandSettingsBean brandSettingsBean = new BrandSettingsBean();
                    brandSettingsBean.setPhoneBrand(sysParam.getParamType());
                    brandSettingsBean.setType(sysParam.getParamKey());
                    brandSettingsBean.setDesc(sysParam.getParamDesc());
                    brandSettingsBean.setContext(sysParam.getParamValue());
                    brandSettingsBean.setBrandSettingsImg(sysParam.getRemark2());
                    brandSettingsBeanList.add(brandSettingsBean);
                }
            }

        } else {
            //没有传类型 直接所以类型的list
            List<SysParam> sysParamList = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, modelSysParam.getParamValue());
            if (sysParamList != null) {
                for (int i = 0; i < sysParamList.size(); i++) {
                    SysParam sysParam = sysParamList.get(i);
                    BrandSettingsBean brandSettingsBean = new BrandSettingsBean();
                    brandSettingsBean.setPhoneBrand(sysParam.getParamType());
                    brandSettingsBean.setType(sysParam.getParamKey());
                    brandSettingsBean.setDesc(sysParam.getParamDesc());
                    brandSettingsBean.setContext(sysParam.getParamValue());
                    brandSettingsBean.setBrandSettingsImg(sysParam.getRemark2());
                    brandSettingsBeanList.add(brandSettingsBean);
                }
            }

        }
        /* 排序 */
        brandSettingsBeanList = brandSettingsBeanList.stream().sorted(Comparator.comparing(BrandSettingsBean::getType)).collect(Collectors.toList());

        QueryPhoneSettingsResponse queryPhoneSettingsResponse = new QueryPhoneSettingsResponse();
        /**查询域名*/
        SysParam bankUrlSysParam = sysParamService.getSysParamInfo(instId, BspsAppParamConstant.PHONE_SETTINGS, BspsAppParamConstant.BANK_URL);
        if (bankUrlSysParam != null) {
            queryPhoneSettingsResponse.setBankUrl(bankUrlSysParam.getParamValue());
        }
        queryPhoneSettingsResponse.setBrandSettingsList(brandSettingsBeanList);
        /* 重组返回结果 */
        BizResponse<QueryPhoneSettingsResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryPhoneSettingsResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        QueryPhoneSettingsResponse mchntQrcodeQueryResponse = (QueryPhoneSettingsResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntQrcodeQueryResponse);
    }

}
